/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.arena;

import com.cmp.arena.controls.BallControl;
import com.cmp.arena.controls.LightingControl;
import com.cmp.arena.controls.TimeControl;
import com.cmp.core.Main;
import com.cmp.core.constants.CollisionGroupsConstants;
import com.cmp.player.PlayerFactory;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.scene.Node;
import com.jme3.scene.SceneGraphVisitor;
import com.jme3.scene.Spatial;

/**
 * Create map nodes here.
 * That includes everything map related like triggers and powerups.
 * 
 * @author krummer
 */
public class MapFactory {
    
    public static void makeMap(Node target,PhysicsSpace world ,AssetManager assets) {
        //add map controls yo.
        target.setUserData("isMap", true);
        target.addControl(new TimeControl());
        
        //load geometry
        Spatial sceneModel =  assets.loadModel("Scenes/arenaScene.j3o");
        
        //make physics
        CollisionShape sceneShape = 
            CollisionShapeFactory.createMeshShape((Node) sceneModel);
        RigidBodyControl landscape = new RigidBodyControl(sceneShape, 0);
        sceneModel.addControl(landscape);
        landscape.setFriction(10.f);
        landscape.setCollisionGroup(CollisionGroupsConstants.MAP);
        landscape.setCollideWithGroups(CollisionGroupsConstants.ALL);
        target.attachChild(sceneModel);
        world.add(landscape);
        
        //add lighting
        target.addControl(new LightingControl());
        
        //set MapNode
        //target= (Node) ((Node) sceneModel).getChild("Map");
        
        //search for markers and resolve them
        findGoal(target);
        findBall(target);
    }
    
    private static void findGoal(Node map) {
        
        SceneGraphVisitor goalVisitor = new SceneGraphVisitor() {

            public void visit(Spatial spatial) {
                if(spatial.getUserData("isGoal") != null) {
                    Node goal = new Node();
                    goal.setLocalTransform(spatial.getLocalTransform().clone());
                    ((Node) spatial).attachChild(goal);
                    boolean team = spatial.getUserData("Team");
                    GoalFactory.makeGoal(goal,team);
                    Main.app.getStateManager().getState(Arena.class).addNode(goal);
                }  
            }
        };
        
        map.breadthFirstTraversal(goalVisitor);  
    }
    
    private static void findBall(Node map) {
        
        SceneGraphVisitor ballVisitor = new SceneGraphVisitor() {

            public void visit(Spatial spatial) {
                
                if(spatial.getUserData("isBallSpawn") != null) {
                    Node ball = new Node("Ball");
                    ((Node) spatial).attachChild(ball);
                    BallFactory.makeBall(ball, Main.app.getAssetManager());
                    ball.setLocalTranslation(spatial.getLocalTranslation().clone());
                    ball.getControl(BallControl.class).setSpawnPos(spatial.getLocalTranslation().clone());
                    Main.app.getStateManager().getState(Arena.class).addNode(ball);
                }
                
            }
            
        };
        map.breadthFirstTraversal(ballVisitor);
    }
}
